<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('muser');
   $this->load->library('form_validation');
   $this->load->library('pagination');
 }
 function index(){ 	
 	 if ($this->input->post('Customer_ID')!=NULL)
 	 {
 	 	$this->form_validation->set_rules('Customer_ID','Customer_ID','trim|integer|callback_check_Customer');
		$this->form_validation->set_rules('CustomerName','CustomerName','trim|required');  
		$this->form_validation->set_rules('Country','Country','trim|required'); 
		$this->form_validation->set_rules('Lab_ID','Lab_ID','trim|required'); 		
		if ($this->form_validation->run()==FALSE){
			$data['error']='Insert fall.Please try again!';
		}else{
			$DT['Customer_ID']=$this->input->post('Customer_ID');	
		 	$DT['CustomerName']=$this->input->post('CustomerName');
			$DT['Country']=$this->input->post('Country');
			$DT['Lab_ID']=$this->input->post('Lab_ID');
			$this->muser->addData('Customer',$DT);
			$data['error']='insert successful!';
		} 	
			
 	 }	
 	 $config['base_url'] = base_url().'customer/index'; 
	 $config['total_rows'] = $this->muser->num_rows('Customer');
	 
	 $config['per_page'] = 5; // xác định số record ở mỗi trang  
     $config['uri_segment'] = 3; // xác định segment chứa page number  
	 $this->pagination->initialize($config); 
	 $data['result']=$this->muser->getalldata('Customer',$config['per_page'],$this->uri->segment(3));	
	 $lab1=$this->muser->getData('Lab');
	 foreach ($lab1 as $row) {
		 $lab[$row->Lab_ID]=$row->Lab_ID.'-'.$row->LabName;
	 }
	 $data['lab']=$lab;
	 $this->load->view('_header');
     $this->load->view('customer/customer_view',$data); 
     $this->load->view('_footer');
 }
 function delete($Customer_ID){
 //	$Customer_ID=$_GET('Customer_ID');
	$this->muser->delColumn('Customer','Customer_ID',$Customer_ID);
	redirect('customer/index');
 }
 
 function edit(){
 	$editLabID=$this->session->userdata('Lab_ID');
	redirect('/lab/edit',$editLabID);
 }
 
 function check_Customer($str){
 		return $this->muser->checkData('Customer','Customer_ID',$str);		
 }
}